package com.itzyf.pokemondata.activity.setting

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.AsyncTask
import android.os.Environment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.bumptech.glide.Glide
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.fragment.PayFragment
import com.itzyf.pokemondata.utils.FileUtil
import com.tydic.baselibrary.activity.BaseActivity
import com.tydic.baselibrary.contant.Constant
import kotlinx.android.synthetic.main.activity_reward.*
import java.io.File

/**
 * @author 依风听雨
 * @version 创建时间：2019/02/07 20:40
 */
class RewardActivity : BaseActivity() {

    private var type = 0

    override fun handlerActivity() {
        vpContent.adapter = PayPageAdapter(supportFragmentManager)
        tlTabs.setupWithViewPager(vpContent)

    }

    override fun getContentViewId() = R.layout.activity_reward

    override fun getToolbarTitle() = "打赏作者"


    class PayPageAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
        private val page = arrayListOf(PayFragment.newInstance(0), PayFragment.newInstance(1))
        private val titles = arrayListOf("微信", "支付宝")
        override fun getItem(position: Int) = page[position]

        override fun getCount() = page.size

        override fun getPageTitle(position: Int) = titles[position]
    }

    fun saveImage(type: Int) {
        this.type = type
        if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            doSDCardPermission()
        } else {
            requestPermission(
                Constant.WRITE_READ_EXTERNAL_CODE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        }
    }

    override fun doSDCardPermission() {
        download()
    }


    // 保存图片到手机
    private fun download() {

        object : AsyncTask<Void, Int, File>() {

            override fun doInBackground(vararg params: Void): File? {
                var file: File? = null
                try {
                    val future = Glide
                        .with(this@RewardActivity)
                        .load(if (type == 0) R.drawable.ic_wechat else R.drawable.ic_zhifubao)
                        .downloadOnly(
                            com.bumptech.glide.request.target.Target.SIZE_ORIGINAL,
                            com.bumptech.glide.request.target.Target.SIZE_ORIGINAL
                        )

                    file = future.get()

                    // 首先保存图片
                    val pictureFolder =
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absoluteFile

                    val appDir = File(pictureFolder, "fuke")
                    if (!appDir.exists()) {
                        appDir.mkdirs()
                    }
                    val fileName = System.currentTimeMillis().toString() + ".jpg"
                    val destFile = File(appDir, fileName)

                    FileUtil.CopyFile(file, destFile)

                    // 最后通知图库更新
                    sendBroadcast(
                        Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                            Uri.fromFile(File(destFile.getPath()))
                        )
                    )


                } catch (e: Exception) {
                    e.printStackTrace()
                }

                return file
            }

            override fun onPostExecute(file: File) {
                showToast("保存成功")
            }

        }.execute()
    }
}